package front

import (
	"github.com/gin-gonic/gin"
	"net/http"
	Staff "supervideo/app/models/staff"
	video2 "supervideo/app/models/video"
	"supervideo/app/models/video_staff_map"
	"supervideo/pkg/database"
)

type DetailController struct {
}

func (detail DetailController) Detail(c *gin.Context) {

	var video video2.Video
	database.DB.Model(video2.Video{}).Where("status", 1).First(&video)

	// 导演
	var directorIds []string
	database.DB.Model(video_staff_map.VideoStaffMap{}).Where("video_id = ? and type = ?", video.ID, 1).Pluck("staff_id", &directorIds)
	var directors []Staff.Staff
	database.DB.Model(Staff.Staff{}).Where("id in ?", directorIds).Find(&directors)

	// 主演
	var leadActorIds []string
	database.DB.Model(video_staff_map.VideoStaffMap{}).Where("video_id = ? and type = ?", video.ID, 2).Pluck("staff_id", &leadActorIds)
	var leadActors []Staff.Staff
	database.DB.Model(Staff.Staff{}).Where("id in ?", leadActorIds).Find(&leadActors)

	// 编剧
	var screenWriterIds []string
	database.DB.Model(video_staff_map.VideoStaffMap{}).Where("video_id = ? and type = ?", video.ID, 3).Pluck("staff_id", &screenWriterIds)
	var screenWriters []Staff.Staff
	database.DB.Model(Staff.Staff{}).Where("id in ?", screenWriterIds).Find(&screenWriters)

	var recommendVideos []video2.Video
	// 同类型推荐
	database.DB.Order("rand()").Where("zone_id = ? and status = ?", video.ZoneId, 1).Limit(10).Find(&recommendVideos)

	var guessVideos []video2.Video
	database.DB.Order("rand()").Where("status = ?", 1).Limit(4).Find(&guessVideos)

	c.HTML(http.StatusOK, "detail.html", gin.H{
		"Title":           "详情",
		"video":           video,
		"recommendVideos": recommendVideos,
		"guessVideos":     guessVideos,
		"leadActors":      leadActors,
		"directors":       directors,
		"screenWriters":   screenWriters,
	})
}
